using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomController:MonoBehaviour {

	[SerializeField] float fadeinSpeed;
	[SerializeField] float fadeoutSpeed;

	SpriteRenderer sprite;
	float opacity = 1;
	bool contactPlayer;

	private void Awake() {
		sprite=GetComponent<SpriteRenderer>();
	}

	private void FixedUpdate() {
		if(contactPlayer) {
			contactPlayer=false;
			opacity-=fadeinSpeed*Time.deltaTime;
		} else {
			opacity+=fadeoutSpeed*Time.deltaTime;
		}
		opacity=Mathf.Clamp01(opacity);
		sprite.color=new Color(1,1,1,opacity);
	}

	private void OnTriggerStay2D(Collider2D collision) {
		if(!collision.GetComponent<PlayerController>()) return;
		contactPlayer=true;
	}

}
